﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinFormsMvp;
using WinFormsMvp.Forms;
using EngineLayer;
using PresentationLayer.Views.ViewContracts;
using PresentationLayer.Presenters;
using PresentationLayer.Properties;
using System.Globalization;
using EngineLayer.Services;



namespace PresentationLayer.Views
{
    [PresenterBinding(typeof(MainPresenter))]
    public class MainView : MvpForm<MainViewModel>, IMainView
    {   
        private UserRights userRights;

        #region PrivateDeclarations
            private StatusStrip statusMain;
            private ToolStripMenuItem salirToolStripMenuItem;
            private ToolStripMenuItem administradorToolStripMenuItem;
            private ToolStripMenuItem usuariosToolStripMenuItem1;
            private ToolStripMenuItem sistemaToolStripMenuItem;
            private ToolStripMenuItem corteDeCajaToolStripMenuItem;
            private ToolStripMenuItem catalógosToolStripMenuItem;
            private ToolStripMenuItem productosToolStripMenuItem;
            private ToolStripMenuItem clientesToolStripMenuItem;
            private ToolStripMenuItem cajaToolStripMenuItem;
            private ToolStripMenuItem ventaToolStripMenuItem;
            public MenuStrip menuMain;
            private ToolStripMenuItem reportesToolStripMenuItem;
            private ToolStripMenuItem envioDeComunicadosToolStripMenuItem;
            private ToolStripStatusLabel tssStatus;
            private ToolStripStatusLabel tssUser;
            public MenuStrip menuLeft;
            public MenuStrip menuRight;
            private ToolStripMenuItem acercaDeToolStripMenuItem;
            private ToolStripMenuItem importDeVentasToolStripMenuItem;
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            userRights = new UserRights();
        //    userRights.TotalRights = Program.User.TotalRights;
            InitializeComponent();
           
            SetPermissions();

            if (userRights.TotalRights == (int)ItemRights.Caja)
                SendKeys.SendWait("^v");
        }

        private void OnClickMenu(object sender, EventArgs e)
        {
            if (sender.ToString() == Resources.Salir)
            {
                CloseFormClicked(null, EventArgs.Empty);
            }
            else
            {
                if (sender.ToString() == Resources.Configuracion)
                {
                    Model.FormToDisplay = typeof(ConfigurationsForm);
                }
                else if (sender.ToString() == Resources.AcercaDe)
                {
                    Model.FormToDisplay = typeof(AboutForm);
                }
                else if (sender.ToString() == Resources.Productos)
                {
                    Model.FormToDisplay = typeof(ProductsCatalogsForm);
                }
                DisplayForms(null, EventArgs.Empty);
            }
        }

        private void InitializeComponent()
        {
            this.statusMain = new StatusStrip();
            this.tssStatus = new ToolStripStatusLabel();
            this.tssUser = new ToolStripStatusLabel();
            this.salirToolStripMenuItem = new ToolStripMenuItem();
            this.administradorToolStripMenuItem = new ToolStripMenuItem();
            this.sistemaToolStripMenuItem = new ToolStripMenuItem();
            this.corteDeCajaToolStripMenuItem = new ToolStripMenuItem();
            this.usuariosToolStripMenuItem1 = new ToolStripMenuItem();
            this.importDeVentasToolStripMenuItem = new ToolStripMenuItem();
            this.catalógosToolStripMenuItem = new ToolStripMenuItem();
            this.clientesToolStripMenuItem = new ToolStripMenuItem();
            this.productosToolStripMenuItem = new ToolStripMenuItem();
            this.cajaToolStripMenuItem = new ToolStripMenuItem();
            this.ventaToolStripMenuItem = new ToolStripMenuItem();
            this.menuMain = new MenuStrip();
            this.reportesToolStripMenuItem = new ToolStripMenuItem();
            this.envioDeComunicadosToolStripMenuItem = new ToolStripMenuItem();
            this.acercaDeToolStripMenuItem = new ToolStripMenuItem();
            this.menuRight = new MenuStrip();
            this.menuLeft = new MenuStrip();
            this.statusMain.SuspendLayout();
            this.menuMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // statusMain
            // 
            this.statusMain.Items.AddRange(new ToolStripItem[] {
            this.tssStatus,
            this.tssUser});
            this.statusMain.Location = new System.Drawing.Point(30, 666);
            this.statusMain.Name = "statusMain";
            this.statusMain.RenderMode = ToolStripRenderMode.Professional;
            this.statusMain.Size = new System.Drawing.Size(587, 22);
            this.statusMain.TabIndex = 0;
            this.statusMain.Text = "statusStrip1";
            // 
            // tssStatus
            // 
            this.tssStatus.AutoSize = false;
            this.tssStatus.BorderStyle = Border3DStyle.Bump;
            this.tssStatus.Name = "tssStatus";
            this.tssStatus.Size = new System.Drawing.Size(542, 17);
            this.tssStatus.Spring = true;
            this.tssStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // tssUser
            // 
            this.tssUser.BorderStyle = Border3DStyle.Bump;
            this.tssUser.MergeAction = MergeAction.MatchOnly;
            this.tssUser.Name = "tssUser";
            this.tssUser.Size = new System.Drawing.Size(30, 17);
       //     this.tssUser.Text = string.Format(CultureInfo.CurrentCulture, "{0} ({1})", Program.User.Name, Program.User.Description);
            // 
            // salirToolStripMenuItem
            // 
            this.salirToolStripMenuItem.Name = "salirToolStripMenuItem";
            this.salirToolStripMenuItem.ShortcutKeys = ((Keys)((Keys.Control | Keys.S)));
            this.salirToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
            this.salirToolStripMenuItem.Text = Resources.Salir;
            this.salirToolStripMenuItem.Click += new System.EventHandler(this.OnClickMenu);
            // 
            // administradorToolStripMenuItem
            // 
            this.administradorToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
            this.sistemaToolStripMenuItem,
            this.corteDeCajaToolStripMenuItem,
            this.usuariosToolStripMenuItem1,
            this.importDeVentasToolStripMenuItem});
            this.administradorToolStripMenuItem.Name = "administradorToolStripMenuItem";
            this.administradorToolStripMenuItem.Size = new System.Drawing.Size(95, 20);
            this.administradorToolStripMenuItem.Text = "&Administrador";
            // 
            // sistemaToolStripMenuItem
            // 
            this.sistemaToolStripMenuItem.Name = "sistemaToolStripMenuItem";
            this.sistemaToolStripMenuItem.Size = new System.Drawing.Size(186, 22);
            this.sistemaToolStripMenuItem.Text = Resources.Configuracion;
            this.sistemaToolStripMenuItem.Click += new System.EventHandler(this.OnClickMenu);
            // 
            // corteDeCajaToolStripMenuItem
            // 
            this.corteDeCajaToolStripMenuItem.Name = "corteDeCajaToolStripMenuItem";
            this.corteDeCajaToolStripMenuItem.ShortcutKeys = ((Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
            this.corteDeCajaToolStripMenuItem.Size = new System.Drawing.Size(186, 22);
            this.corteDeCajaToolStripMenuItem.Text = "Corte de Caja";
        //    this.corteDeCajaToolStripMenuItem.Click += new System.EventHandler(this.corteDeCajaToolStripMenuItem_Click);
            // 
            // usuariosToolStripMenuItem1
            // 
            this.usuariosToolStripMenuItem1.Name = "usuariosToolStripMenuItem1";
            this.usuariosToolStripMenuItem1.ShortcutKeys = ((Keys)((Keys.Control | Keys.U)));
            this.usuariosToolStripMenuItem1.Size = new System.Drawing.Size(186, 22);
            this.usuariosToolStripMenuItem1.Text = "Usuarios";
      //      this.usuariosToolStripMenuItem1.Click += new System.EventHandler(this.usuariosToolStripMenuItem1_Click);
            // 
            // importDeVentasToolStripMenuItem
            // 
            this.importDeVentasToolStripMenuItem.Name = "importDeVentasToolStripMenuItem";
            this.importDeVentasToolStripMenuItem.Size = new System.Drawing.Size(186, 22);
            this.importDeVentasToolStripMenuItem.Text = "Import de Ventas";
       //     this.importDeVentasToolStripMenuItem.Click += new System.EventHandler(this.importDeVentasToolStripMenuItem_Click);
            // 
            // catalógosToolStripMenuItem
            // 
            this.catalógosToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.clientesToolStripMenuItem,
            this.productosToolStripMenuItem});
            this.catalógosToolStripMenuItem.Name = "catalógosToolStripMenuItem";
            this.catalógosToolStripMenuItem.Size = new System.Drawing.Size(72, 20);
            this.catalógosToolStripMenuItem.Text = "Catál&ogos";
            // 
            // clientesToolStripMenuItem
            // 
            this.clientesToolStripMenuItem.Name = "clientesToolStripMenuItem";
            this.clientesToolStripMenuItem.Size = new System.Drawing.Size(128, 22);
            this.clientesToolStripMenuItem.Text = Resources.Clientes;
            this.clientesToolStripMenuItem.Click += new System.EventHandler(this.OnClickMenu);
            // 
            // productosToolStripMenuItem
            // 
            this.productosToolStripMenuItem.Name = "productosToolStripMenuItem";
            this.productosToolStripMenuItem.Size = new System.Drawing.Size(128, 22);
            this.productosToolStripMenuItem.Text = Resources.Productos;
            this.productosToolStripMenuItem.Click += new System.EventHandler(this.OnClickMenu);
            // 
            // cajaToolStripMenuItem
            // 
            this.cajaToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.ventaToolStripMenuItem});
            this.cajaToolStripMenuItem.Name = "cajaToolStripMenuItem";
            this.cajaToolStripMenuItem.Size = new System.Drawing.Size(42, 20);
            this.cajaToolStripMenuItem.Text = "&Caja";
            // 
            // ventaToolStripMenuItem
            // 
            this.ventaToolStripMenuItem.Name = "ventaToolStripMenuItem";
            this.ventaToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)));
            this.ventaToolStripMenuItem.Size = new System.Drawing.Size(145, 22);
            this.ventaToolStripMenuItem.Text = "Venta";
            //this.ventaToolStripMenuItem.Click += new System.EventHandler(this.ventaToolStripMenuItem_Click);
            // 
            // menuMain
            // 
            this.menuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.salirToolStripMenuItem,
            this.administradorToolStripMenuItem,
            this.catalógosToolStripMenuItem,
            this.cajaToolStripMenuItem,
            this.reportesToolStripMenuItem,
            this.envioDeComunicadosToolStripMenuItem,
            this.acercaDeToolStripMenuItem});
            this.menuMain.Location = new System.Drawing.Point(0, 0);
            this.menuMain.Name = "menuMain";
            this.menuMain.Size = new System.Drawing.Size(617, 24);
            this.menuMain.TabIndex = 3;
            this.menuMain.Text = "menuStrip2";
            // 
            // reportesToolStripMenuItem
            // 
            this.reportesToolStripMenuItem.Name = "reportesToolStripMenuItem";
            this.reportesToolStripMenuItem.Size = new System.Drawing.Size(65, 20);
            this.reportesToolStripMenuItem.Text = "&Reportes";
            // 
            // envioDeComunicadosToolStripMenuItem
            // 
            this.envioDeComunicadosToolStripMenuItem.Name = "envioDeComunicadosToolStripMenuItem";
            this.envioDeComunicadosToolStripMenuItem.Size = new System.Drawing.Size(93, 20);
            this.envioDeComunicadosToolStripMenuItem.Text = "Co&municados";
            // 
            // acercaDeToolStripMenuItem
            // 
            this.acercaDeToolStripMenuItem.Name = "acercaDeToolStripMenuItem";
            this.acercaDeToolStripMenuItem.Size = new System.Drawing.Size(79, 20);
            this.acercaDeToolStripMenuItem.Text = Resources.AcercaDe;
            this.acercaDeToolStripMenuItem.Click += new System.EventHandler(this.OnClickMenu);
            // 
            // menuRight
            // 
            this.menuRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.menuRight.Location = new System.Drawing.Point(617, 0);
            this.menuRight.Name = "menuRight";
            this.menuRight.Size = new System.Drawing.Size(30, 688);
            this.menuRight.TabIndex = 5;
            this.menuRight.Text = "menuStrip3";
            // 
            // menuLeft
            // 
            this.menuLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.menuLeft.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.menuLeft.Location = new System.Drawing.Point(0, 24);
            this.menuLeft.Name = "menuLeft";
            this.menuLeft.Size = new System.Drawing.Size(30, 664);
            this.menuLeft.TabIndex = 1;
            this.menuLeft.Text = "menuStrip1";
            // 
              
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
         //   this.BackgroundImage = Resources.logoSplash;
       //     this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.ClientSize = new System.Drawing.Size(647, 688);
            this.Controls.Add(this.statusMain);
            this.Controls.Add(this.menuLeft);
            this.Controls.Add(this.menuMain);
            this.Controls.Add(this.menuRight);
            this.DoubleBuffered = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.IsMdiContainer = true;
            this.Name = "MainView";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.statusMain.ResumeLayout(false);
            this.statusMain.PerformLayout();
            this.menuMain.ResumeLayout(false);
            this.menuMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void SetPermissions()
        {//FIX
            //administradorToolStripMenuItem.Enabled = userRights.HaveRightItem(ItemRights.Administrador);
            //catalógosToolStripMenuItem.Enabled = userRights.HaveRightItem(ItemRights.Catalogos) && userRights.ActivateFranchise();
            //cajaToolStripMenuItem.Enabled = userRights.HaveRightItem(ItemRights.Caja) && userRights.ActivateFranchise();
            //reportesToolStripMenuItem.Enabled = userRights.HaveRightItem(ItemRights.Reportes) && userRights.ActivateFranchise();
            //envioDeComunicadosToolStripMenuItem.Enabled = userRights.HaveRightItem(ItemRights.Comunicados) && userRights.ActivateFranchise();
            //corteDeCajaToolStripMenuItem.Enabled = userRights.ActivateFranchise();
        }

        #region Implementation of IMainView

        public event EventHandler CloseFormClicked;
        public event EventHandler DisplayForms;

        public void DisplayView()
        {
            if (Model.FormToDisplay != null)
            {
                Form MdiChild;
                Type typeOfFormToLoad = Model.FormToDisplay;

                MdiChild = Activator.CreateInstance(typeOfFormToLoad) as Form;
                MdiChild.MdiParent = this;
                MdiChild.Show();

                Model.FormToDisplay = null;
            }
        }

        public void Exit()
        {
            this.Close();
        }

        #endregion

    }
}
